<template>
    <view class="h-[53px] flex justify-between items-center label-search">
        <view class="w-[120px] max-md:hidden">
            <uni-data-select v-model="category" :localdata="categoryData" :clear="false" />
        </view>
        <view class="flex-1">
            <uni-easyinput v-model="keyword" :placeholder="$t('common.search.placeholder')" :inputBorder="false" />
        </view>
        <view class="banner-search-button" @click="onSearch">
            <uni-icons type="search" size="21" color="#fff" />
        </view>
    </view>
</template>
<script setup>
import { navigateTo } from "@/utils/tools"
import { ref } from "vue"
import { useI18n } from "vue-i18n"

const { t } = useI18n()

const keyword = ref("")
const category = ref(0)
const categoryData = ref([
    {
        text: t("common.search.all"),
        value: 0
    },
    {
        text: t("common.search.block"),
        value: 1
    },
    {
        text: t("common.search.address"),
        value: 2
    },
    {
        text: t("common.search.transaction"),
        value: 3
    }
])

const onSearch = () => {
    if (!keyword.value) {
        return uni.$u.toast(t("common.search.emptyKeywords"))
    }

    let page = ""
    let param = ""
    if (category.value == 0) {
        // 默认address, 简单根据长度判断
        if (keyword.value.length > 70) {
            page = "transaction"
            param = `?tx=${keyword.value}`
        } else if (keyword.value.length > 30) {
            page = "address"
            param = `?address=${keyword.value}`
        } else {
            page = "block"
            param = `?height=${keyword.value}`
        }
    } else if (category.value == 1) {
        page = "block"
        param = `?height=${keyword.value}`
    } else if (category.value == 2) {
        page = "address"
        param = `?address=${keyword.value}`
    } else if (category.value == 3) {
        page = "transaction"
        param = `?tx=${keyword.value}`
    } else {
        return
    }
    navigateTo(`/pages/index/${page}${param}`)
}
</script>
